package my.edu.model.User.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import jakarta.validation.groups.Default;
import my.edu.common.domain.IdsRequest;
import my.edu.common.domain.PageDTO;
import my.edu.common.exception.AdminException;
import my.edu.common.exception.UserLoginException;
import my.edu.common.validate.IntegerPattern;
import my.edu.common.validate.groups.Insert;
import my.edu.model.User.entity.User;
import my.edu.model.User.entity.UserRole;
import my.edu.model.User.entity.VO.LoginVO;
import my.edu.model.User.entity.dto.*;
import my.edu.model.User.service.IRoleService;
import my.edu.model.User.service.IUserRoleService;
import my.edu.model.User.service.IUserService;
import my.edu.common.domain.Response;
import my.edu.security.userEntity.UserDetail;
import my.edu.utils.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/user")
@Tag(name = "用户接口")
@Validated
public class UserController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IUserRoleService userRoleService;

    @PutMapping
    @RequiresPermissions(value = {"system:user"})
    @Operation(summary = "新增用户")
    public Response saveUser(@RequestBody @Validated(value = Insert.class) UserDTO userDTO){
        userService.saveUser(userDTO);
        return Response.success();
    }

    @PostMapping("/updateUser")
    @RequiresPermissions(value = {"system:user"})
    @Operation(summary = "更新用户")
    public Response updateUser(@RequestBody @Validated(value = Default.class) UserDTO userDTO){
        userService.updateUser(userDTO);
        return Response.success();
    }

    @PostMapping("/batchSetStatus")
    @RequiresPermissions(value = {"system:user"})
    @Operation(summary = "更新用户状态")
    public Response batchSetStatus(@RequestBody JSONObject jsonObject){
        int status = jsonObject.getInt("status");
        String ids = jsonObject.getStr("ids");
        if(status!=0&&status!=1){
            return Response.error("状态值错误");
        }
        List<Long> longs = StringUtils.strToLongList(ids);
        userService.setStatus(longs,status);
        return Response.success();
    }

    @PutMapping("/updatePwd")
    @Operation(summary = "更新用户密码")
    public Response updateUserPwd(@RequestBody @Validated UserPwdDTO userPwdDTO) {
        if (!userPwdDTO.getPassword().equals(userPwdDTO.getConfirmPassword()))throw new UserLoginException("两次密码不一致");
       userService.updateUserPwd(userPwdDTO.getOldpassword(),userPwdDTO.getPassword());
        return Response.success();
    }

    @PostMapping("/resetPwd")
    @RequiresPermissions(value = {"system:user"})
    @Operation(summary = "重置用户密码")
    public Response updateUserPwdR(
            @NotNull(message = "用户id不能为空")
            @RequestBody Map<String, Object> id) {
        userService.reUserPwd(id);
        return Response.success();
    }

    @GetMapping("/list")
    @Operation(summary = "查询员工")
    @RequiresPermissions(value = {"system:user"})
    public Response list(@ModelAttribute @Validated PageDTO page,  @ModelAttribute UserSearchDTO search){
        return Response.success(userService.listAllUser(page,search));
    }

    @GetMapping
    @Operation(summary = "查询所有员工")
    public Response listAll(){
        return Response.success(userService.listAllUserOnly());
    }

    @GetMapping("/one")
    @Operation(summary = "根据id查询员工")
    @RequiresPermissions(value = {"system:user"})
    public Response getVOById( Long id) {
        return Response.success(userService.getVOById(id));
    }

    @DeleteMapping
    @RequiresPermissions(value = {"system:user"})
    @Operation(summary = "删除用户")
    @Transactional
    public Response delete( Long id) {
        User byId = userService.getById(id);
        if (byId.getUsername().equals("admin"))throw new AdminException("超级管理员不能删除");
        userService.removeById(id);
        userRoleService.remove(Wrappers.<UserRole>lambdaQuery().eq(UserRole::getUserId, id));
        return Response.success();
    }
}